package com.yhl.vocation.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhl.vocation.entity.pojo.Admin;
import com.yhl.vocation.entity.pojo.Class;
import com.yhl.vocation.entity.pojo.Student;
import com.yhl.vocation.service.*;
import com.yhl.vocation.util.ResUtil;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class AdminController {

    private AdminService adminService;

    private MenuService menuService;

    private ClassService classService;

    private StudentService studentService;

    private VacationService vacationService;

    public AdminController(AdminService adminService, MenuService menuService, ClassService classService, StudentService studentService, VacationService vacationService) {
        this.adminService = adminService;
        this.menuService = menuService;
        this.classService = classService;
        this.studentService = studentService;
        this.vacationService = vacationService;
    }

    @GetMapping("login")
    public String login(String username, String password){
        Admin admin = adminService.login(username,password);
        if (admin != null){
            Map<String, Object> res = new HashMap<>();
            res.put("menus",menuService.getMenus(admin.getType()));
            res.put("user", admin);
            return ResUtil.build("200", res);
        }
        return ResUtil.build("250", "账号或者密码错误");
    }

    @GetMapping("list")
    public String list(){
        List<Admin> admins = adminService.listTeach(2);
        return ResUtil.build("200", admins);
    }

    @PostMapping("add")
    public String add(@RequestBody Admin admin){
        if (adminService.save(admin)) {
            return ResUtil.build("200", "添加成功");
        }else {
            return ResUtil.build("200", "添加失败");
        }
    }

    @PostMapping("update")
    public String update(@RequestBody Admin admin){
        if (adminService.updateById(admin)) {
            return ResUtil.build("200", "更新成功");
        }else {
            return ResUtil.build("200", "更新失败");
        }
    }

    @GetMapping("delete")
    public String delete(String id){
        long l = Long.parseLong(id);
        if (adminService.removeById(id)) {
            return ResUtil.build("200", "删除成功");
        }else {
            return ResUtil.build("200", "删除失败");

        }
    }

    @GetMapping("class")
    public String listClass(){
        return ResUtil.build("200", classService.listClass());
    }

    @GetMapping("teaches")
    public String teaches(){
        return ResUtil.build("200", adminService.listTech());
    }

    @PostMapping("addClass")
    public String addClass(@RequestBody Class clas){
        if (classService.save(clas)) {
            return ResUtil.build("200","保存成功");
        }else {
            return ResUtil.build("200","保存失败");
        }
    }

    @PostMapping("updateClass")
    public String updateClass(@RequestBody Class clas){
        if (classService.updateById(clas)) {
            return ResUtil.build("200","更新成功");
        }else {
            return ResUtil.build("200","更新失败");
        }
    }

    @GetMapping("deleteClass")
    public String deleteClass(String id){
        if (classService.removeById(id)) {
            return ResUtil.build("200","删除成功");
        }else {
            return ResUtil.build("200","删除失败");
        }
    }

    @GetMapping("my")
    public String my(String id){
        return ResUtil.build("200",classService.list(new LambdaQueryWrapper<Class>().eq(Class::getTeachId,id)));
    }

    @GetMapping("students")
    public String students(String id){
        return ResUtil.build("200",studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getClassId,id)));
    }

    @PostMapping("addStudent")
    public String addStudent(@RequestBody Student student){
        if (studentService.save(student)) {
            return ResUtil.build("200","保存成功");
        }else {
            return ResUtil.build("400","保存失败");
        }
    }

    @PostMapping("updateStudent")
    public String updateStudent(@RequestBody Student student){
        if (studentService.updateById(student)) {
            return ResUtil.build("200","更新成功");
        }else {
            return ResUtil.build("400","更新失败");
        }
    }

    @GetMapping("deleteStudent")
    public String deleteStudent(String id){
        if (studentService.removeById(id)) {
            return ResUtil.build("200","删除成功");
        }else {
            return ResUtil.build("400","删除失败");
        }
    }

    @GetMapping("listVocation")
    public String listVocation(String id, String type){
        return ResUtil.build("200",vacationService.listVocation(id,type));
    }

    @GetMapping("updateVacation")
    public String updateVacation(String id, String type){
        if (vacationService.updateVacation(id,type)){
            return ResUtil.build("200","操作成功");
        }else {
            return ResUtil.build("200","操作失败");
        }
    }
}
